// pages/commissionDetail/index.js
const common = require('../../utils/common');
const {
  getShopCommissionDetail
} = require('../../services/distribution');
const defaultPage = {
  curPage: 1,
  pageSize: 10,
  noMore: false
};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    segIndex: 0, // 默认选择第一个
    todayQuery: {
      ...defaultPage,
      ...{
        type: 1
      }
    },
    beforeQuery: {
      ...defaultPage,
      ...{
        type: 2
      }
    },
    allQuery: {
      ...defaultPage,
      ...{
        type: 3
      }
    },
    // 今日提成
    todayInfo: {
      commissionAmount: '',
      timestamp: '',
      list: []
    },
    // 前七日
    beforeInfo: {
      commissionAmount: '',
      timestamp: '',
      list: []
    },
    // 累计
    allInfo: {
      commissionAmount: '',
      timestamp: '',
      list: []
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu({});
    // 获取数据
    this.onPullDownRefresh();
  },


  // 获取今日订单数
  requestToGetShopCommissionDetail({
    curPage,
    pageSize,
    timestamp,
    type
  }, pullDownRefresh) {
    let params = {
      curPage: curPage,
      pageSize: pageSize,
      type: type
    };
    if (timestamp) {
      params.timestamp = timestamp
    };
    getShopCommissionDetail(params).then(res => {
      if (pullDownRefresh) {
        wx.stopPullDownRefresh({});
      }
      this.hanleCommissionDetailData(res, curPage, type, pullDownRefresh);
    }).catch(err => {
      if (pullDownRefresh) {
        wx.stopPullDownRefresh({});
      }
      common.log(err);
    })
  },

  // 处理返回的数据
  hanleCommissionDetailData(res, curPage, type, pullDownRefresh) {
    if (type == 1) {
      //当天
      //设置数据
      let info = this.data.todayInfo;
      let tmpList = info.list;
      if (pullDownRefresh) {
        tmpList = [];
      }
      //pageInfo
      let tmpPageInfo = this.data.todayQuery;
      if (res.page.pages <= curPage) {
        tmpPageInfo.noMore = true;
      } else {
        tmpPageInfo.curPage++;
      }
      //list
      if (res.data.length > 0) {
        tmpList = [...tmpList, ...res.data];
      }
      info.list = tmpList;
      info.commissionAmount = res.commissionAmount;
      info.timestamp = res.timestamp;
      this.setData({
        todayQuery: tmpPageInfo,
        todayInfo: info
      });
    } else if (type == 2) {
      //最近7天
      //设置数据
      let info = this.data.beforeInfo;
      let tmpList = info.list;
      if (pullDownRefresh) {
        tmpList = [];
      }
      //pageInfo
      let tmpPageInfo = this.data.beforeQuery;
      if (res.page.pages <= curPage) {
        tmpPageInfo.noMore = true;
      } else {
        tmpPageInfo.curPage++;
      }
      //list
      if (res.data.length > 0) {
        tmpList = [...tmpList, ...res.data];
      }
      info.list = tmpList;
      info.commissionAmount = res.commissionAmount;
      info.timestamp = res.timestamp;
      this.setData({
        beforeQuery: tmpPageInfo,
        beforeInfo: info
      });
    } else if (type == 3) {
      //累计
      //设置数据
      let info = this.data.allInfo;
      let tmpList = info.list;
      if (pullDownRefresh) {
        tmpList = [];
      }
      //pageInfo
      let tmpPageInfo = this.data.allQuery;
      if (res.page.pages <= curPage) {
        tmpPageInfo.noMore = true;
      } else {
        tmpPageInfo.curPage++;
      }
      //list
      if (res.data.length > 0) {
        tmpList = [...tmpList, ...res.data];
      }
      info.list = tmpList;
      info.commissionAmount = res.commissionAmount;
      info.timestamp = res.timestamp;
      this.setData({
        allQuery: tmpPageInfo,
        allInfo: info
      });
    }
  },
  queryChange: function (payload, pullDownRefresh) {
    if (this.data.segIndex == 0) {
      // 今日
      let query = {
        ...this.data.todayQuery,
        ...payload
      };
      this.setData({
        todayQuery: query
      });
      // 
      let params = query;
      if (payload.curPage != 1) {
        params.timestamp = this.data.todayInfo.timestamp;
      } else {
        delete params.timestamp;
      }
      this.requestToGetShopCommissionDetail(params, pullDownRefresh);
    } else if (this.data.segIndex == 1) {
      // 前七日
      let query = {
        ...this.data.beforeQuery,
        ...payload
      };
      this.setData({
        beforeQuery: query
      });
      // 
      let params = query;
      if (payload.curPage != 1) {
        params.timestamp = this.data.beforeInfo.timestamp;
      } else {
        delete params.timestamp;
      }
      this.requestToGetShopCommissionDetail(params, pullDownRefresh);
    } else if (this.data.segIndex == 2) {
      // 累计
      let query = {
        ...this.data.allQuery,
        ...payload
      };
      this.setData({
        allQuery: query
      });
      // 
      let params = query;
      if (payload.curPage != 1) {
        params.timestamp = this.data.allInfo.timestamp;
      } else {
        delete params.timestamp;
      }
      this.requestToGetShopCommissionDetail(params, pullDownRefresh);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.queryChange(defaultPage, true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.segIndex == 0) {
      if (this.data.todayQuery.noMore) return;
      this.queryChange(this.data.todayQuery);
    } else if (this.data.segIndex == 1) {
      if (this.data.beforeQuery.noMore) return;
      this.queryChange(this.data.beforeQuery);
    } else if (this.data.segIndex == 2) {
      if (this.data.allQuery.noMore) return;
      this.queryChange(this.data.allQuery);
    }
  },

  //seg选择
  onSeg(e) {
    let index = parseInt(e.currentTarget.dataset.index);
    if (index == this.data.segIndex) return;
    this.setData({
      segIndex: index
    })
    // 
    this.onPullDownRefresh();
  }
})
